<?php

namespace App\Http\Servers\Admin;

use App\PageInfo;
use App\PageLabel;

class PageServer
{
    private $imageserver;
    private $pageinfo;

    public function __construct(ImageServer $imageServer, PageInfo $pageInfo)
    {
        $this->imageserver = $imageServer;
        $this->pageinfo = $pageInfo;
    }

    //没有权限删除
    public function nopower($author)
    {
        return false;
        if (!((bool)\Auth::guard('admin')->user()->is_root || \Auth::guard('admin')->user()->id == $author)) {
            return message(false, '删除失败', '该模块需要超级管理员或本模块作者的权限才能删除!');
        } else {
            return false;
        }
    }

    //因存在信息无法删除
    public function hasinfo($id)
    {
        if ($this->pageinfo->where('label', $id)->count() > 0) {
            return message(false, '删除失败', '该模块下存在信息,无法删除!');
        } else {
            return false;
        }
    }

    //删除前安全验证
    public function sofeVerify($id, $author)
    {
        if ($message = $this->nopower($author)) {
            return $message;
        }

        if ($message = $this->hasinfo($id)) {
            return $message;
        }

        return false;
    }

    //得到企业信息模块的模版
    public function getPageinfoTheme()
    {
        $tmpPageThemes = glob(config('app.page_theme'));
        foreach ($tmpPageThemes as $theme) {
            $pageThemes[] = explode('.', basename($theme))[0];
        }

        return $pageThemes;
    }

    //处理添加企业信息
    public function getAddPageinfo($request)
    {
        $create['name'] = $request->name;
        $create['label'] = $request->label;
        $create['theme'] = $request->theme;
        $create['page_view'] = $request->page_view;
        $create['tag'] = $request->tag;
        $create['xindex'] = $request->xindex;
        $create['describe'] = $request->describe;
        $create['author'] = \Auth::guard('admin')->user()->id;
        $create['jump_url'] = $request->jump_url;
        $create['status'] = $request->has('status') ? 1 : 0;
        $create['extend_field_1'] = $request->extend_field_1;
        $create['extend_field_2'] = $request->extend_field_2;
        $create['extend_field_3'] = $request->extend_field_3;
        $create['extend_field_4'] = $request->extend_field_4;
        if ($request->has('simpledescribe')) {
            $create['simpledescribe'] = $request->simpledescribe;
        } else {
            $create['simpledescribe'] = str_replace(['&nbsp;'], [''], subtext(strip_tags($request->describe), config('app.simple_len')));
        }
        if ($request->hasFile('image')) {
            $create['image'] = $this->imageserver->uploadImage($request->image, 'page_info', 'page_info');
        }

        return $create;
    }

    //处理更新企业信息
    public function getUpdatePageinfo($request)
    {
        $update['name'] = $request->name;
        $update['label'] = $request->label;
        $update['theme'] = $request->theme;
        $update['page_view'] = $request->page_view;
        $update['tag'] = $request->tag;
        $update['xindex'] = $request->xindex;
        $update['describe'] = $request->describe;
        $update['author'] = \Auth::guard('admin')->user()->id;
        $update['jump_url'] = $request->jump_url;
        $update['status'] = $request->has('status') ? 1 : 0;
        $update['extend_field_1'] = $request->extend_field_1;
        $update['extend_field_2'] = $request->extend_field_2;
        $update['extend_field_3'] = $request->extend_field_3;
        $update['extend_field_4'] = $request->extend_field_4;
        if ($request->has('simpledescribe')) {
            $update['simpledescribe'] = $request->simpledescribe;
        } else {
            $update['simpledescribe'] = str_replace(['&nbsp;'], [''], subtext(strip_tags($request->describe), config('app.simple_len')));
        }
        if ($request->hasFile('image')) {
            $update['image'] = $this->imageserver->uploadImage($request->image, 'page_info', 'page_info');
        }

        return $update;
    }

    //得到信息模块的模版
    public function getInfoTheme()
    {
        $tmpInfoThemes = glob(config('app.page_theme'));
        foreach ($tmpInfoThemes as $theme) {
            $infoThemes[] = explode('.', basename($theme))[0];
        }

        return $infoThemes;
    }
}